//
//  XTStatusViewModel.swift
//  Weibo(Swift)
//
//  Created by 肖霆 on 16/5/13.
//  Copyright © 2016年 肖霆. All rights reserved.
//

import UIKit
//每个cell对应的viewModel
class XTStatusViewModel: NSObject {
    var status:XTStatus?
    //转发微博内容
    var retweetContent :String?
    //转发数
    var retweetCount:String?
    //评论数
    var commentCount:String?
    //赞
    var unlikeCount:String?
    //来源
    var sourceContent : String?
    //会员等级图片
    var mbrankImage:UIImage?
    
    //认证类型图片
    var VerifiedTypeImage:UIImage?
    //时间框内容
    //处理显示的微博发布的时间
    var timeContent:String?{
    
        guard let CreatTime = status?.created_at else {
            return nil
        }
        //先把字符串转换为一个时间对象，再将时间对象进行格式处理变成字符串
        return NSDate.sinceData(CreatTime).sinaDateString
        
    }
    
    //原创微博表情富文本
    var originalAttributedString:NSMutableAttributedString?
    //转发微博表情富文本
    var reweetAttributedString:NSMutableAttributedString?
    
    init(status:XTStatus) {
        super.init()
        self.status = status
        handleRetweetContent(status)
        //处理“转发数”数据
        retweetCount = handleCount(status.reposts_count, defaultTitle: "转发")
        //处理“评论数”数据
        commentCount = handleCount(status.comments_count, defaultTitle: "评论")
        //处理“点赞数”数据
        unlikeCount = handleCount(status.attitudes_count, defaultTitle: "赞")
        
        //处理来源数据
        handleSourceContent(status)
        //处理会员等级数据
        handleMBrankImage(status.user?.mbrank ?? 0)
        //处理认证类别
        handleVerifiedTypeImage(status.user?.verified_type ?? 0)
        //处理表情字符串
        originalAttributedString = handleEmoticonContentWithStatus(status.text!)
    }
    //将显示的微博内容中的表情字符串转化为富文本显示
    private func handleEmoticonContentWithStatus(status:String)->NSMutableAttributedString{
        
        let result = NSMutableAttributedString(string: status)
        var matchResultArray = [XTMatchResult]()
    
        (status as NSString).enumerateStringsMatchedByRegex("\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]") { (captureCount, captureString, captureRange, _) -> Void in
            if let emoticonStr = captureString.memory as? String{
                let matchResult = XTMatchResult(string: emoticonStr, range: captureRange.memory)
                matchResultArray.append(matchResult)
            }
        }
        //倒序便利
        for value in matchResultArray.reverse(){
            if let emoticon = XTEmoticonTool.sharedTools.emoticonWithEmoticonString(value.string) {
                //  创建表情富文本
                let emoticonAttr = NSAttributedString.attributedWithEmotion(emoticon, font: UIFont.systemFontOfSize(14))
                print(status)
                //  替换表情富文本
                result.replaceCharactersInRange(value.range, withAttributedString: emoticonAttr)
            }
        }
        return result
        }

    
    //转发内容，拼接字符串方法
    private func handleRetweetContent(status:XTStatus){
    
        guard let text = status.retweeted_status?.text else{
        return
        }
        
        guard let name = status.retweeted_status?.user?.screen_name else{
        return
            
        }
        
        retweetContent = "@\(name):\(text)"
        reweetAttributedString = handleEmoticonContentWithStatus(retweetContent!)
    
    }
    
    //处理转发、评论、赞的数据的方法
    private func handleCount(count:Int,defaultTitle:String)->String{
        if count>0{
            
            if count>10000{
            let result = CGFloat(count/1000/10)
                let resultStr:String = "\(result)万"
                
                if resultStr.containsString(".0"){
                return resultStr.stringByReplacingOccurrencesOfString(".0", withString: "")
                }
                return resultStr
            }else{
                return "\(count)"
            }
            
            
        }else{
            return defaultTitle
        }
    }
    
    //处理来源的数据的方法
    private func handleSourceContent(status:XTStatus){
    
        guard let source = status.source else{
            return
        }
        
        if source.containsString("\">"){
            let startIndex = source.rangeOfString("\">")?.endIndex
            let endIndex = source.rangeOfString("</")?.startIndex
            
            if let start = startIndex, end = endIndex{
                sourceContent = source.substringWithRange(start..<end)
}
        }
    
    }
    //处理会员等级图片的方法
    private func handleMBrankImage(count:Int){
        if count > 0 && count < 7 {
        mbrankImage = UIImage(named: "common_icon_membership_level\(count)")
        }
    }
    
    //处理认证类别的方法
    private func handleVerifiedTypeImage(VerifiedType:Int){
    
        switch VerifiedType{
//            认证类型 -1 没有认证 ，0 认证用户，2，3，5 企业认证 ， 220 达人
        case 0:
            VerifiedTypeImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            VerifiedTypeImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            VerifiedTypeImage = UIImage(named: "avatar_grassroot")
        default:
            VerifiedTypeImage = nil
            
        }
    }
    
}
